package com.ssy.controller;

import cn.hutool.core.util.StrUtil;
import com.ssy.common.Result;
import com.ssy.service.ScoreService;
import com.ssy.service.impl.ScoreServiceImpl;
import com.ssy.util.RespUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

/**
 * ScoreServlet
 *
 * @author ssy
 * @date 2020/11/21
 **/
@WebServlet(urlPatterns = {"/score.do"})
public class ScoreServlet extends HttpServlet implements Serializable {

    private static final long serialVersionUID = -8698979658038042028L;

    ScoreService scoreService = new ScoreServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("scoreList".equals(type)) {
            scoreList(req, resp);
        } else if ("addScore".equals(type)) {
            addScore(req, resp);
        } else if ("updateScore".equals(type)) {
            updateScore(req, resp);
        } else if ("deleteOneScore".equals(type)) {
            deleteOneScore(req, resp);
        } else if ("bathDeleteScore".equals(type)) {
            bathDeleteScore(req, resp);
        }

    }

    /**
     * 批量删除成绩
     *
     * @param req
     * @param resp
     */
    private void bathDeleteScore(HttpServletRequest req, HttpServletResponse resp) {
        String[] ids = req.getParameterValues("id");
        Result result = this.scoreService.BathdeleteScore(ids);
        RespUtil.write(resp, result);
    }

    /**
     * 删除成绩
     *
     * @param req
     * @param resp
     */
    private void deleteOneScore(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Result result = this.scoreService.deleteOneScore(id);
        RespUtil.write(resp, result);
    }

    /**
     * 修改
     *
     * @param req
     * @param resp
     */
    private void updateScore(HttpServletRequest req, HttpServletResponse resp) {
        String studentNumberId = req.getParameter("studentNumber");
        String courseNameId = req.getParameter("courseName");
        String fraction = req.getParameter("fraction");
        String scoreId = req.getParameter("scoreId");
        Result result = this.scoreService.updateScore(studentNumberId, courseNameId, fraction, scoreId);
        RespUtil.write(resp, result);

    }

    /**
     * 添加成绩
     *
     * @param req
     * @param resp
     */
    private void addScore(HttpServletRequest req, HttpServletResponse resp) {
        String studentNumberId = req.getParameter("studentNumber");
        String courseNameId = req.getParameter("courseName");
        String fraction = req.getParameter("fraction");
        Result result = this.scoreService.addScore(studentNumberId, courseNameId, fraction);
        RespUtil.write(resp, result);

    }

    /**
     * 加载成绩列表
     *
     * @param req
     * @param resp
     */
    private void scoreList(HttpServletRequest req, HttpServletResponse resp) {
        String studentName = req.getParameter("studentName");
        String studentNumberId = req.getParameter("studentNumber");
        String className = req.getParameter("className");
        String teacherName = req.getParameter("teacherName");
        String majorName = req.getParameter("majorName");
        String courseName = req.getParameter("courseName");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");

        HashMap<String, Object> param = new HashMap<>();
        if (StrUtil.isNotBlank(studentName)) {
            param.put("studentName", studentName);
        }
        if (StrUtil.isNotBlank(studentNumberId)) {
            param.put("studentNumberId", studentNumberId);
        }
        if (StrUtil.isNotBlank(className)) {
            param.put("className", className);
        }
        if (StrUtil.isNotBlank(teacherName)) {
            param.put("teacherName", teacherName);
        }
        if (StrUtil.isNotBlank(majorName)) {
            param.put("majorName", majorName);
        }
        if (StrUtil.isNotBlank(courseName)) {
            param.put("courseName", courseName);
        }
        Result result = this.scoreService.queryPage(param, page, limit);
        RespUtil.write(resp, result);

    }

}
